package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Oarin
 * @class SystemConfigServiceImpl
 * @date 2023/6/1 19:30
 */
public class ConfigServiceImpl implements ConfigService {
    @Override
    public Map<String, String> listMall() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample example = new MarketSystemExample();
        example.or().andKeyNameLike("market_mall_%").andDeletedEqualTo(false);
        List<MarketSystem> systemList = systemMapper.selectByExample(example);
        Map<String, String> map = systemList.stream().collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));
        sqlSession.close();
        return map;
    }

    @Override
    public int updateConfig(Map<String, String> data) {
        TransactionUtil.startTransaction();
        MarketSystemMapper systemMapper = TransactionUtil.getMapper(MarketSystemMapper.class);
        int i = 0;
        try {
            i = 0;
            // Map<String, String> ConfigMap = new HashMap<>();
            for (Map.Entry<String, String> entry : data.entrySet()) {
                MarketSystemExample example = new MarketSystemExample();
                example.createCriteria().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);

                MarketSystem marketSystem = new MarketSystem();
                //marketSystem.setKeyName(entry.getKey());
                marketSystem.setKeyValue(String.valueOf(entry.getValue()));
                marketSystem.setUpdateTime(new Date());

                i += systemMapper.updateByExampleSelective(marketSystem, example);

            }
        } catch (Exception exception) {
            exception.printStackTrace();
            TransactionUtil.rollback();
        }
        TransactionUtil.commit();
        return i;
    }

    @Override
    public Map<String, String> QueryConfig(String[] strArray) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);

        Map<String, String> map = new HashMap<>();

        for (String keyName : strArray) {
            MarketSystemExample example = new MarketSystemExample();
            example.or().andKeyNameEqualTo(keyName).andDeletedEqualTo(false);
            List<MarketSystem> marketSystems = systemMapper.selectByExample(example);
            MarketSystem marketSystem = marketSystems.get(0);
            map.put(keyName, marketSystem.getKeyValue());
        }

        sqlSession.close();
        return map;
    }

}
